/*
 @description: 超级管理员

 @author: 李建翰

 @update: 
*/
'use strict';
const Controller = require('egg').Controller;
const utility = require("utility")
class SuperAdministratorCpntroller extends Controller{
     // 查询
     async index() { //搜索
        const {ctx,app} = this;
        const {keyname,page=1,number=5} = ctx.request.query
        const offset=(page-1)*number
        let data,count
        if(keyname && page){
            data = await app.mysql.query(`select id,super_name,super_poss from superAdministrator where super_name like '%${keyname}%' limit ${offset},${number}`)
            count=await app.mysql.query(`select count(*) as count from superAdministrator where super_name like '%${keyname}%'`)
        }else if(keyname){
            data = await app.mysql.query(`select id,super_name,super_poss from superAdministrator where super_name ='%${keyname}%' or super_name  like '%${keyname}%' limit ${offset},${number}`)
            count=await app.mysql.query(`select count(*) as count from superAdministrator where super_name like '%${keyname}%' or super_name like '%${keyname}%'`)
        }else{
            data=await app.mysql.query(`select id,super_name,super_poss from superAdministrator limit ${offset},${number}`)
            count=await app.mysql.query(`select count(*) as count from superAdministrator`)
        }

        
    // async index() { //搜索
    //     const {ctx,app} = this;
    //     const {keyname} = ctx.request.query
    //     let where=''
    //     if(keyname){
    //         where=`where ador_name like "%${keyname}%"`
    //     }
    //     const result =await app.mysql.query(`select * from fuxiao_administrators ${where}`)
        ctx.body = {
            code:0,
            msg:"查找成功",
            data,
            count:count
        }
    }

    async show(){
        const {ctx,app} = this;
        const {id} = ctx.params;
        const result=await app.mysql.get('superAdministrator',{ID:id})
        if(result){
            ctx.body={
                code:0,
                msg:'查找成功',
                data:result
            };
        }else{
            ctx.body = {
                code:4007,
                msg:"查找失败"
            }
        }
    }
            
            // 添加
            async create(){     //post
                const { ctx,app} = this;
                const {super_name,super_poss}=ctx.request.body
                if(super_name && super_poss){
                    const data=await app.mysql.insert('superAdministrator',{super_name,super_poss:utility.md5(super_poss)});
                    ctx.body = {
                        code: 0,
                        msg: "新增成功",
                    };
                } else {
                    ctx.body = {
                        code: 4006,
                        msg: "添加失败",
                    };
                }
            }
            //改
            async update() {//put
                const {ctx,app} =this;
                const {super_name,super_poss} = ctx.request.body
                const {id} = ctx.params;
                const row={
                    super_name:super_name,
                    super_poss:utility.md5(super_poss)
                };
                const options={
                    where:{
                        id:+id
                    }
                };
                const result=await app.mysql.update('superAdministrator',row,options);
                    if(result){
                        ctx.body={
                            code:0,
                            msg:"编辑成功",
                        };
                    }else{
                        ctx.body={
                            code:4008,
                            msg:"编辑失败",
                        };
                    } 
            }

            //删除
            async destroy() { //DELETE
                const {ctx,app} = this;
                const {id} = ctx.params;
                const result =await app.mysql.delete(`superAdministrator`,{id:+id})
                if(result){
                    ctx.body = {
                        code: 0,
                        msg: "删除成功",
                    };
                }else{
                    ctx.body = {
                        code: 4007,
                        msg: "删除失败",
                    }; 
                }
                }
}
module.exports = SuperAdministratorCpntroller